package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: health_parent
 * @Package: com.itheima.controller
 * @ClassName: RoleManageController
 * @Author: Torifu
 * @Date: 2019/7/5 18:29
 * @Description:
 * @Version: 1.0
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**@描述 分页查询
     *@参数 [queryPageBean]
     *@返回值 com.itheima.entity.PageResult
     *@创建人 boss
     *@创建时间 2019-7-6 18:52:05
     *@修改人和其它信息
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;

    }

    /**@描述 新增角色
     *@参数 [role, permissionIds, menuIds]
     *@返回值 com.itheima.entity.Result
     *@创建人 boss
     *@创建时间 2019-7-6 18:53:01
     *@修改人和其它信息
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try {
            System.out.println(permissionIds.length);
            System.out.println(menuIds.length);
            roleService.add(role, permissionIds,menuIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

    }

    /**@描述 编辑角色
     *@参数 [role, permissionIds, menuIds]
     *@返回值 com.itheima.entity.Result
     *@创建人 boss
     *@创建时间 2019-7-6 18:53:25
     *@修改人和其它信息
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try {
            System.out.println(permissionIds.length);
            System.out.println(menuIds.length);
            roleService.edit(role, permissionIds,menuIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }

    }

    /**@描述 根据ID查询角色信息
     *@参数 [id]
     *@返回值 com.itheima.entity.Result
     *@创建人 boss
     *@创建时间 2019-7-6 18:53:55
     *@修改人和其它信息
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        try {
            Role role = roleService.findRoleById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**@描述 根据角色ID查询对应的权限id和菜单ID
     *@参数 [id]
     *@返回值 com.itheima.entity.Result
     *@创建人 boss
     *@创建时间 2019-7-6 18:54:12
     *@修改人和其它信息
     */
    @RequestMapping("/findMenuIdsAndPermissionIdsByRoleId")
    public Result findMenuIdsAndPermissionIdsByRoleId(Integer id){
        try {
            Map<String, Integer[]> map =roleService.findMenuIdsAndPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**@描述 删除角色
     *@参数 [id]
     *@返回值 com.itheima.entity.Result
     *@创建人 boss
     *@创建时间 2019-7-6 18:55:04
     *@修改人和其它信息
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Result result = roleService.delete(id);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
